/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.MaintenanceRecordItem;
import com.zhuoyue.elmt.service.MaintenanceRecordItemService;

/**
 * 电梯维保记录明细Controller
 * @author chenhao
 * @version 2016-09-07
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/maintenanceRecordItem")
public class MaintenanceRecordItemController extends BaseController {

	@Autowired
	private MaintenanceRecordItemService maintenanceRecordItemService;
	
	@ModelAttribute
	public MaintenanceRecordItem get(@RequestParam(required=false) String id) {
		MaintenanceRecordItem entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = maintenanceRecordItemService.get(id);
		}
		if (entity == null){
			entity = new MaintenanceRecordItem();
		}
		return entity;
	}
	
	@RequiresPermissions("elmt:maintenanceRecordItem:view")
	@RequestMapping(value = {"list", ""})
	public String list(MaintenanceRecordItem maintenanceRecordItem, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MaintenanceRecordItem> page = maintenanceRecordItemService.findPage(new Page<MaintenanceRecordItem>(request, response), maintenanceRecordItem); 
		model.addAttribute("page", page);
		return "zhuoyue/elmt/maintenanceRecordItemList";
	}

	@RequiresPermissions("elmt:maintenanceRecordItem:view")
	@RequestMapping(value = "form")
	public String form(MaintenanceRecordItem maintenanceRecordItem, Model model) {
		model.addAttribute("maintenanceRecordItem", maintenanceRecordItem);
		return "zhuoyue/elmt/maintenanceRecordItemForm";
	}

	@RequiresPermissions("elmt:maintenanceRecordItem:edit")
	@RequestMapping(value = "save")
	public String save(MaintenanceRecordItem maintenanceRecordItem, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, maintenanceRecordItem)){
			return form(maintenanceRecordItem, model);
		}
		maintenanceRecordItemService.save(maintenanceRecordItem);
		addMessage(redirectAttributes, "保存电梯维保记录明细成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/maintenanceRecordItem/?repage";
	}
	
	@RequiresPermissions("elmt:maintenanceRecordItem:edit")
	@RequestMapping(value = "delete")
	public String delete(MaintenanceRecordItem maintenanceRecordItem, RedirectAttributes redirectAttributes) {
		maintenanceRecordItemService.delete(maintenanceRecordItem);
		addMessage(redirectAttributes, "删除电梯维保记录明细成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/maintenanceRecordItem/?repage";
	}

}